#include "StdAfx.h"
#include "NetNode.h"

NetNode::NetNode(void)
{
	m_protocal = NULL;
}

NetNode::~NetNode(void)
{
	for( int i = 0; i < m_boards.size(); i++ )
	{
		if( m_boards[i] != NULL )
		{
			delete m_boards[i];
		}
	}
	if( m_protocal ) delete m_protocal;
}

int NetNode::get_slot_num( Board * bd )
{
	for( int i = 0; i < m_boards.size(); i++ )
	{
		if( m_boards[i] == bd )
		{
			return (i>7) ? (i-1) : i; 
//			return i;
		}
	}
	return -1;
}

Board * NetNode::AddBoard( Board& b, int pos )
{
	if( pos < 0 || pos >= get_slot_count() )
	{
		return NULL;
	}
	if( pos >= m_boards.size() )
	{
		for( int i = 0; i <= pos; i++ )
		{
			m_boards.push_back(NULL);
		}
	}
	if( m_boards[pos] != NULL ) return NULL;
	m_boards[pos] = new Board(b); //copy constructor
	return m_boards[pos];
}

void NetNode::DelBoard( int pos )
{
	if( pos < 0 || pos >= m_boards.size() )
	{
		return;
	}
	if( m_boards[pos] != NULL ) 
	{
		Board * bd = m_boards[pos];
		m_boards[pos] = NULL;
		delete bd;
	}
}

Board * NetNode::get_board( int pos )
{
	if( pos < 0 || pos >= m_boards.size() )
	{
		return NULL;
	}
	return m_boards[pos];
}

void NetNode::update_state(void)
{
	if( m_protocal == NULL )
		m_protocal = new ProtocalHelper;
	m_protocal->get_node_state( this );
}

bool NetNode::modify_ip( DWORD newIP )
{
	m_newIP = newIP;
	return m_protocal->modify_ip( newIP );
}

void NetNode::modify_mac( CString newmac )
{
	m_newIP = m_IP;
	m_protocal->modify_mac(newmac);
}

void NetNode::modify_type( CString newtype )
{
	m_newIP = m_IP;
	m_protocal->modify_type(newtype);
}

void NetNode::set_board_text( Board * bd, TEXT_INFO info )
{
	m_protocal->set_board_text( get_slot_num(bd),bd, info );
}

void NetNode::set_board_time( Board * bd, TIME_INFO info )
{
	m_protocal->set_board_time( get_slot_num(bd),bd, info );
}
void NetNode::reset(Board * bd)
{
	m_protocal->reset( get_slot_num(bd) );
}
void NetNode::send_short_cmd(Board * bd, unsigned char * cmd, int len)
{
	m_protocal->send_short_cmd(get_slot_num(bd), cmd, len);
}
void NetNode::OnReceive(unsigned char *data, int length)
{
	char a[] = {0x1a, 0xcf, 0xfc, 0x1d, 
		0x00, 0x01};
	for (int i=0; i<length - sizeof(a); ++i)
	{
		if (data[i] == 0x1A && data[i+1] == 0xCF && data[i+2] == 0xFC && data[i+3] == 0x1D)
		{
			if (data[i+4] == a[4] && data[i+5] == a[5])
			{
				m_IP = m_newIP;

			}
			for( int j = 0; j < get_slot_count(); ++j )
			{
				Board * b = get_board( j );
				if( b != NULL )
				{
					b->update_state( data[i+6+j] );
				}
			}
			break;
		}
	}
	//for( int i = 0; i < length && i < get_slot_count(); i++ )
	//{
	//	Board * b = get_board( i );
	//	if( b != NULL )
	//	{
	//		b->update_state( data[i] );
	//	}
	//}
}

bool NetNode::is_connected()
{
	return m_protocal->is_connected();
}

bool NetNode::connect()
{
	return m_protocal->connect();
}

bool NetNode::finished()
{
	return m_protocal->finished();
}